package model

import (
	"course-admin/userrpc/userrpc"

	"gorm.io/gorm"
)

type Collect struct {
	gorm.Model
	UserId  int `gorm:"type:int;not null"`
	GoodsId int `gorm:"type:int;not null"`
}

func (c *Collect) CollectAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collect) FindCollectById(db *gorm.DB, userId int64, goodsId int64) error {
	return db.Debug().Where("userId = ?", userId).Where("goodsId = ?", goodsId).First(c).Error

}

func (c *Collect) CollectDel(db *gorm.DB, in *userrpc.CollectDelRequest) error {
	return db.Debug().Where("user_id = ?", in.UserId).Where("goods_id = ?", in.GoodsId).Delete(c).Error
}
func CollectList(db *gorm.DB, id int64) []*userrpc.CollectList {
	var list []*userrpc.CollectList
	db.Debug().
		Model(&Collect{}).
		Select("goods.*,users.head_img,users.nickname").
		Joins("left join goods on collects.goods_id = goods.id").
		Joins("left join users on collects.user_id = users.id").
		Where("collects.user_id = ?", id).
		Find(&list)
	return list
}
